<?php

use \GatewayWorker\Lib\Gateway;  

require_once dirname(__DIR__) . '/public.class.model.php';
class Play_Model extends Public_Model
{
	
	/*
		叫地主
	*/
	public function askLandlord($arrData)
	{
		$timestamp = time();
		$result = array();
		
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(!isset($data['room_id']) || $data['room_id']<=0)
		{
			$this->logMessage('error', "function(askLandlord):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		
		$room_id = $data['room_id'];
		$become = $data['become'];

		//判断请求合法性
		$is_legal = $this->checkRequestClientLegal($client_id,$room_id,$account_id);
		if(!$is_legal)
		{
			$this->logMessage('error', "function(checkRequestClientLegal):illegal opt: account_id($account_id) room_id($room_id)"." in file".__FILE__." on Line ".__LINE__);
			//推送强制下线
			$illegal_message = $this->_JSON(array("result"=>"-203","result_message"=>"illegal opt")); 
			Gateway::sendToCurrentClient($illegal_message);
			return OPT_CONST::NO_RETURN;
		}


		$active_user = $this->queryActiveUser($room_id);
		if($active_user != $account_id){
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>array('active_user'=>$active_user),"result_message"=>"未轮到你操作");
		}

		$account_status = $this->queryAccountStatus($room_id, $account_id);
		if($account_status != Game::AccountStatus_NotAsk){
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>array(),"result_message"=>"状态异常");
		}
		
		$this->deleteRoomTimer($room_id);
		
		if($become){
			//叫地主
			$Redis_Model = Redis_Model::getModelObject();
			$replyArr = array("[roomid]"=>$room_id);
			$Play_Key = strtr(Redis_Const::Play_Key, $replyArr);
			$mkv[Redis_Const::Play_Field_Landlord] = $account_id;
			$mset_result = $Redis_Model->hmsetField($Play_Key,$mkv);

			//更新用户状态
			$members = $this->queryRoomMembers($room_id);
			if(is_array($members) && count($members)){
				foreach ($members as $member_id) {
					if($member_id == $account_id){
						$rStatus = Game::AccountStatus_Landlord;
						$this->updateAccountStatus($room_id, $member_id, $rStatus);
					} else {
						$rStatus = Game::AccountStatus_Farmer;
						$this->updateAccountStatus($room_id, $member_id, $rStatus);
					}
				}
			}

			//公示地主牌
			$landlord_card = $this->queryLandlordCard($room_id);
			$msg_arr = array(
				'result' => 0,
				'operation' => 'LandlordCard',
				'result_message' => "地主牌",
				'data' => array(
					'landlord_card' => $landlord_card,
					)
				);
			$this->pushMessageToGroup($room_id, $msg_arr);

			//合并地主牌
			$my_card = $this->queryPlayerCard($room_id, $account_id);
			$my_card = array_merge($my_card, $landlord_card);

			$landlord = Landlord::instance();
			$my_card = $landlord->sortCard($my_card);

			$this->setPlayerCard($room_id, $account_id, $my_card);

			//通知出牌
			$this->notyUserToChoose($room_id, $account_id);

		} else {

			$Redis_Model = Redis_Model::getModelObject();
			$replyArr = array("[roomid]"=>$room_id);
			$AccountStatus_Key = strtr(Redis_Const::AccountStatus_Key, $replyArr);
			$mkv[$account_id] = Game::AccountStatus_Farmer;
			$mset_result = $Redis_Model->hmsetField($AccountStatus_Key,$mkv);

			$next_account_id = $this->takeTurns($room_id);
			$status = $this->queryAccountStatus($room_id, $next_account_id);
			if(Game::AccountStatus_NotAsk == $status){
				$this->notyUserToAsk($room_id, $next_account_id);
			} else {
				$room_members = $this->queryRoomMembers($room_id);
				$this->initGame($room_id, $room_members);
			}
		}

		return OPT_CONST::NO_RETURN;
	}

	/*
		出牌
	*/
	public function chooseCard($arrData)
	{
		$timestamp = time();
		$result = array();
		
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(empty($data['room_id']) || $data['room_id']<=0)
		{
			$this->writeLog("function(chooseCard):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		
		$room_id = $data['room_id'];
		$card = $data['card'];

		//判断请求合法性
		$is_legal = $this->checkRequestClientLegal($client_id,$room_id,$account_id);
		if(!$is_legal)
		{
			$this->logMessage('error', "function(checkRequestClientLegal):illegal opt: account_id($account_id) room_id($room_id)"." in file".__FILE__." on Line ".__LINE__);
			//推送强制下线
			$illegal_message = $this->_JSON(array("result"=>"-203","result_message"=>"illegal opt")); 
			Gateway::sendToCurrentClient($illegal_message);
			return OPT_CONST::NO_RETURN;
		}

		$active_user = $this->queryActiveUser($room_id);
		if($active_user != $account_id){
			return array("result"=>"-2","operation"=>$operation,"data"=>array('active_user'=>$active_user),"result_message"=>"未轮到你操作");
		}

		$hand_card_array = $this->queryPlayerCard($room_id, $account_id);
		foreach ($card as $value) {
			if(!in_array($value, $hand_card_array)){
				return array("result"=>"-2","operation"=>$operation,"data"=>array('card'=>$value),"result_message"=>"请刷新获取正确手牌");
			}
		}
		
		$Redis_Model = Redis_Model::getModelObject();
		$currentCard = $this->queryCurrentCard($room_id);
		
		$this->deleteRoomTimer($room_id);

		$landlord = Landlord::instance();
		$card_type_info = $landlord->getCardType($card);
		$card_type = $card_type_info['type'];

		if($card_type == -1){	//错误牌型
			return array("result"=> -2,"operation"=>$operation,"data"=>$result,"result_message"=>"错误牌型");
		}

		$can = $landlord->canThrowOut($currentCard, $card);
		if(!$can){
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>$result,"result_message"=>"打不起");
		}

		$is_pass = count($card) == 0 ? true : false;  //过牌 

		//更新当前出牌
		if(!$is_pass){	
			$this->updateCurrentCard($room_id, $account_id, $card);
			$this->removePlayerCard($room_id, $account_id, $card);
			
			if(in_array($card_type, [4,14])){
				$this->doubleMultiple($room_id); //炸弹翻倍
			}

			if($this->queryLandlord($room_id) == $account_id){	//地主出牌
				$notPassCount = $this->increaseNotPassCount($room_id, true);
				$this->writeLog("[$room_id] ($account_id) 地主出牌次数:".$notPassCount);
			} else {	//农民出牌
				$notPassCount = $this->increaseNotPassCount($room_id, false);
				$this->writeLog("[$room_id] ($account_id) 农民出牌次数:".$notPassCount);
			}

		} else {	//pass 则不需更新

		}
		
		$msg_arr = array(
			'result' => 0,
			'operation' => 'ThrowOutCard',
			'result_message' => "玩家出牌",
			'data' => array(
				'account_id' => $account_id,
				'card'  => $card,
				'card_type' => $card_type,
				'is_passive' => 0
				)
			);
		$this->pushMessageToGroup($room_id, $msg_arr);

		$next_account_id = $this->takeTurns($room_id);

		if(!$is_pass){
			$card_count = $this->queryPlayerCardCount($room_id, $account_id);
			
			$this->writeLog("[$room_id] ($account_id) 打牌, 剩余牌数:".$card_count);
			if($card_count == 0){	//出完牌  判胜负
				//显示本局输赢
				$this->writeLog("[$room_id] ($account_id) 打完牌了");
				$this->showWin($room_id);
				return OPT_CONST::NO_RETURN;
			} 
		} else { //pass
			$currentCardUser =  $this->queryCurrentCardUser($room_id);
			if($currentCardUser == $next_account_id){  //重置当前桌面牌为空,当前牌用户为-1
				$this->updateCurrentCard($room_id, -1, array());
			}
		}
		$this->notyUserToChoose($room_id, $next_account_id);
		return OPT_CONST::NO_RETURN;
	}

	

	/*
		发送声音
	*/
	public function broadcastVoice($arrData)
	{
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}
		
		if(!isset($data['room_id']) || $data['room_id']<=0)
		{
			$this->logMessage('error', "function(broadcastVoice):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		
		$room_id = $data['room_id'];
		$voice_num = $data['voice_num'];

		//判断请求合法性
		$is_legal = $this->checkRequestClientLegal($client_id,$room_id,$account_id);
		if(!$is_legal)
		{
			$this->logMessage('error', "function(checkRequestClientLegal):illegal opt: account_id($account_id) room_id($room_id)"." in file".__FILE__." on Line ".__LINE__);
			//推送强制下线
			$illegal_message = $this->_JSON(array("result"=>"-203","result_message"=>"illegal opt")); 
			Gateway::sendToCurrentClient($illegal_message);
			return OPT_CONST::NO_RETURN;
		}
		
		$msg_arr = array("result"=>"0","operation"=>$operation,"data"=>array(
			'account_id'=>$account_id,
			'voice_num' =>$voice_num
			),"result_message"=>"发送声音");
		$this->pushMessageToGroup($room_id, $msg_arr, $client_id);
		return OPT_CONST::NO_RETURN;
	}


	/*
		获取在线数
	*/
	public function LxhServer($arrData)
	{
		$data                   = $arrData['data'];
		$operation              = $arrData['operation'];
		$client_count = Gateway::getAllClientCount();
		return array("result"=>0,"operation"=>$operation,"data"=>array('client_count'=>$client_count),"result_message"=>"当前在线连接总数");
	}
	
}
